home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / sptmbr16.lha / defclass.lisp < prev    next >
Lisp/Scheme  |  1992-12-21  |  17KB  |  461 lines

  1. ;;;-*-Mode:LISP; Package: PCL; Base:10; Syntax:Common-lisp -*-
  2. ;;;
  3. ;;; *************************************************************************
  4. ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
  5. ;;; All rights reserved.
  6. ;;;
  7. ;;; Use and copying of this software and preparation of derivative works
  8. ;;; based upon this software are permitted.  Any distribution of this
  9. ;;; software or derivative works must comply with all applicable United
  10. ;;; States export control laws.
  11. ;;; 
  12. ;;; This software is made available AS IS, and Xerox Corporation makes no
  13. ;;; warranty about the software, its performance or its conformity to any
  14. ;;; specification.
  15. ;;; 
  16. ;;; Any person obtaining a copy of this software is requested to send their
  17. ;;; name and post office or electronic mail address to:
  18. ;;;   CommonLoops Coordinator
  19. ;;;   Xerox PARC
  20. ;;;   3333 Coyote Hill Rd.
  21. ;;;   Palo Alto, CA 94304
  22. ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
  23. ;;;
  24. ;;; Suggestions, comments and requests for improvements are also welcome.
  25. ;;; *************************************************************************
  26. ;;;
  27.  
  28. (in-package :pcl)
  29.  
  30. ;;;
  31. ;;; MAKE-TOP-LEVEL-FORM is used by all PCL macros that appear `at top-level'.
  32. ;;;
  33. ;;; The original motiviation for this function was to deal with the bug in
  34. ;;; the Genera compiler that prevents lambda expressions in top-level forms
  35. ;;; other than DEFUN from being compiled.
  36. ;;;
  37. ;;; Now this function is used to grab other functionality as well.  This
  38. ;;; includes:
  39. ;;;   - Preventing the grouping of top-level forms.  For example, a
  40. ;;;     DEFCLASS followed by a DEFMETHOD may not want to be grouped
  41. ;;;     into the same top-level form.
  42. ;;;   - Telling the programming environment what the pretty version
  43. ;;;     of the name of this form is.  This is used by WARN.
  44. ;;; 
  45. (defun make-top-level-form (name times form)
  46.   (flet ((definition-name ()
  47.        (if (and (listp name)
  48.             (memq (car name) '(defmethod defclass class method method-combination)))
  49.            (format nil "~A~{ ~S~}"
  50.                (capitalize-words (car name) ()) (cdr name))
  51.            (format nil "~S" name))))
  52.     (definition-name)
  53.     #+Genera
  54.     (progn
  55.       #-Genera-Release-8
  56.       (let ((thunk-name (gensym "TOP-LEVEL-FORM")))
  57.     `(eval-when ,times
  58.        (defun ,thunk-name ()
  59.          (declare (sys:function-parent
  60.             ,(cond ((listp name)
  61.                 (case (first name)
  62.                   (defmethod `(method ,@(rest name)))
  63.                   (otherwise (second name))))
  64.                    (t name))
  65.             ,(cond ((listp name)
  66.                 (case (first name)
  67.                   ((defmethod defgeneric) 'defun)
  68.                   ((defclass) 'defclass)
  69.                   (otherwise (first name))))
  70.                    (t 'defun))))
  71.          ,form)
  72.        (,thunk-name)))
  73.       #+Genera-Release-8
  74.       `(compiler-let ((compiler:default-warning-function ',name))
  75.      (eval-when ,times
  76.        (funcall #'(lambda ()
  77.             (declare ,(cond ((listp name)
  78.                      (case (first name)
  79.                        ((defclass)
  80.                         `(sys:function-parent ,(second name) defclass))
  81.                        ((defmethod)
  82.                         `(sys:function-name (method ,@(rest name))))
  83.                        ((defgeneric)
  84.                         `(sys:function-name ,(second name)))
  85.                        (otherwise
  86.                          `(sys:function-name ,name))))
  87.                     (t
  88.                      `(sys:function-name ,name))))
  89.             ,form)))))
  90.     #+LCL3.0
  91.     `(compiler-let ((lucid::*compiler-message-string*
  92.               (or lucid::*compiler-message-string*
  93.               ,(definition-name))))
  94.        (eval-when ,times ,form))
  95.     #+cmu
  96.     (if (member 'compile times)
  97.         `(eval-when ,times ,form)
  98.         form)
  99.     #+kcl
  100.     (let* ((*print-pretty* nil)
  101.            (thunk-name (gensym (definition-name))))
  102.       (gensym "G") ; set the prefix back to something less confusing.
  103.       `(eval-when ,times
  104.          (defun ,thunk-name ()
  105.            ,form)
  106.          (,thunk-name)))
  107.     #-(or Genera LCL3.0 cmu kcl)
  108.     (make-progn `',name `(eval-when ,times ,form))))
  109.  
  110. (defun make-progn (&rest forms)
  111.   (let ((progn-form nil))
  112.     (labels ((collect-forms (forms)
  113.            (unless (null forms)
  114.          (collect-forms (cdr forms))
  115.          (if (and (listp (car forms))
  116.               (eq (caar forms) 'progn))
  117.              (collect-forms (cdar forms))
  118.              (push (car forms) progn-form)))))
  119.       (collect-forms forms)
  120.       (cons 'progn progn-form))))
  121.  
  122.  
  123.  
  124. ;;; 
  125. ;;; Like the DEFMETHOD macro, the expansion of the DEFCLASS macro is fixed.
  126. ;;; DEFCLASS always expands into a call to LOAD-DEFCLASS.  Until the meta-
  127. ;;; braid is set up, LOAD-DEFCLASS has a special definition which simply
  128. ;;; collects all class definitions up, when the metabraid is initialized it
  129. ;;; is done from those class definitions.
  130. ;;;
  131. ;;; After the metabraid has been setup, and the protocol for defining classes
  132. ;;; has been defined, the real definition of LOAD-DEFCLASS is installed by the
  133. ;;; file defclass.lisp
  134. ;;; 
  135. (defmacro DEFCLASS (name direct-superclasses direct-slots &rest options)
  136.   (declare (indentation 2 4 3 1))
  137.   (expand-defclass name direct-superclasses direct-slots options))
  138.  
  139. (defun expand-defclass (name supers slots options)
  140.   (declare (special *defclass-times* *boot-state* *the-class-structure-class*))
  141.   (setq supers  (copy-tree supers)
  142.     slots   (copy-tree slots)
  143.     options (copy-tree options))
  144.   (let ((metaclass 'standard-class))
  145.     (dolist (option options)
  146.       (if (not (listp option))
  147.           (error "~S is not a legal defclass option." option)
  148.           (when (eq (car option) ':metaclass)
  149.             (unless (legal-class-name-p (cadr option))
  150.               (error "The value of the :metaclass option (~S) is not a~%~
  151.                       legal class name."
  152.                      (cadr option)))
  153.             (setq metaclass (cadr option))
  154.         (setf options (remove option options))
  155.         (return t))))
  156.  
  157.     (let ((*initfunctions* ())
  158.           (*accessors* ())                         ;Truly a crock, but we got
  159.           (*readers* ())                           ;to have it to live nicely.
  160.           (*writers* ()))
  161.       (declare (special *initfunctions* *accessors* *readers* *writers*))
  162.       (let ((canonical-slots
  163.           (mapcar #'(lambda (spec)
  164.               (canonicalize-slot-specification name spec))
  165.               slots))
  166.         (other-initargs
  167.           (mapcar #'(lambda (option)
  168.               (canonicalize-defclass-option name option))
  169.               options))
  170.         (defstruct-p (and (eq *boot-state* 'complete)
  171.                   (let ((mclass (find-class metaclass nil)))
  172.                 (and mclass
  173.                      (*subtypep mclass 
  174.                         *the-class-structure-class*))))))
  175.     (do-standard-defsetfs-for-defclass *accessors*)
  176.         (let ((defclass-form 
  177.                  (make-top-level-form `(defclass ,name)
  178.                    (if defstruct-p '(load eval) *defclass-times*)
  179.            `(progn
  180.               ,@(mapcar #'(lambda (x)
  181.                     `(declaim (ftype (function (t) t) ,x)))
  182.                 #+cmu *readers* #-cmu nil)
  183.               ,@(mapcar #'(lambda (x)
  184.                     #-setf (when (consp x)
  185.                          (setq x (get-setf-function-name (cadr x))))
  186.                     `(declaim (ftype (function (t t) t) ,x)))
  187.                 #+cmu *writers* #-cmu nil)
  188.               (let ,(mapcar #'cdr *initfunctions*)
  189.             (load-defclass ',name
  190.                        ',metaclass
  191.                        ',supers
  192.                        (list ,@canonical-slots)
  193.                        (list ,@(apply #'append 
  194.                               (when defstruct-p
  195.                             '(:from-defclass-p t))
  196.                               other-initargs))
  197.                        ',*accessors*))))))
  198.           (if defstruct-p
  199.               (progn
  200.                 (eval defclass-form) ; define the class now, so that
  201.                 `(progn              ; the defstruct can be compiled.
  202.                    ,(class-defstruct-form (find-class name))
  203.                    ,defclass-form))
  204.           (progn
  205.         (when (and (eq *boot-state* 'complete)
  206.                (not (member 'compile *defclass-times*)))
  207.           (inform-type-system-about-std-class name))
  208.         defclass-form)))))))
  209.  
  210. (defun make-initfunction (initform)
  211.   (declare (special *initfunctions*))
  212.   (cond ((or (eq initform 't)
  213.          (equal initform ''t))
  214.      '(function true))
  215.     ((or (eq initform 'nil)
  216.          (equal initform ''nil))
  217.      '(function false))
  218.     ((or (eql initform '0)
  219.          (equal initform ''0))
  220.      '(function zero))
  221.     (t
  222.      (let ((entry (assoc initform *initfunctions* :test #'equal)))
  223.        (unless entry
  224.          (setq entry (list initform
  225.                    (gensym)
  226.                    `(function (lambda () ,initform))))
  227.          (push entry *initfunctions*))
  228.        (cadr entry)))))
  229.  
  230. (defun canonicalize-slot-specification (class-name spec)
  231.   (declare (special *accessors* *readers* *writers*))
  232.   (cond ((and (symbolp spec)
  233.           (not (keywordp spec))
  234.           (not (memq spec '(t nil))))           
  235.      `'(:name ,spec))
  236.     ((not (consp spec))
  237.      (error "~S is not a legal slot specification." spec))
  238.     ((null (cdr spec))
  239.      `'(:name ,(car spec)))
  240.     ((null (cddr spec))
  241.      (error "In DEFCLASS ~S, the slot specification ~S is obsolete.~%~
  242.                  Convert it to ~S"
  243.         class-name spec (list (car spec) :initform (cadr spec))))
  244.     (t
  245.      (let* ((name (pop spec))
  246.         (readers ())
  247.         (writers ())
  248.         (initargs ())
  249.         (unsupplied (list nil))
  250.         (initform (getf spec :initform unsupplied)))
  251.        (doplist (key val) spec
  252.          (case key
  253.            (:accessor (push val *accessors*)
  254.               (push val readers)
  255.               (push `(setf ,val) writers))
  256.            (:reader   (push val readers))
  257.            (:writer   (push val writers))
  258.            (:initarg  (push val initargs))))
  259.        (loop (unless (remf spec :accessor) (return)))
  260.        (loop (unless (remf spec :reader)   (return)))
  261.        (loop (unless (remf spec :writer)   (return)))
  262.        (loop (unless (remf spec :initarg)  (return)))
  263.            (setq *writers* (append writers *writers*))
  264.            (setq *readers* (append readers *readers*))
  265.        (setq spec `(:name     ',name
  266.             :readers  ',readers
  267.             :writers  ',writers
  268.             :initargs ',initargs
  269.             ',spec))
  270.        (if (eq initform unsupplied)
  271.            `(list* ,@spec)
  272.            `(list* :initfunction ,(make-initfunction initform) ,@spec))))))
  273.                         
  274. (defun canonicalize-defclass-option (class-name option)  
  275.   (declare (ignore class-name))
  276.   (case (car option)
  277.     (:default-initargs
  278.       (let ((canonical ()))
  279.     (let (key val (tail (cdr option)))
  280.       (loop (when (null tail) (return nil))
  281.         (setq key (pop tail)
  282.               val (pop tail))
  283.         (push ``(,',key ,,(make-initfunction val) ,',val) canonical))
  284.       `(':direct-default-initargs (list ,@(nreverse canonical))))))
  285.     (otherwise
  286.       `(',(car option) ',(cdr option)))))
  287.  
  288.  
  289. ;;;
  290. ;;; This is the early definition of load-defclass.  It just collects up all
  291. ;;; the class definitions in a list.  Later, in the file braid1.lisp, these
  292. ;;; are actually defined.
  293. ;;;
  294.  
  295.  
  296. ;;;
  297. ;;; Each entry in *early-class-definitions* is an early-class-definition.
  298. ;;; 
  299. ;;;
  300. (defparameter *early-class-definitions* ())
  301.  
  302. (defun early-class-definition (class-name)
  303.   (or (find class-name *early-class-definitions* :key #'ecd-class-name)
  304.       (error "~S is not a class in *early-class-definitions*." class-name)))
  305.  
  306. (defun make-early-class-definition
  307.        (name source metaclass
  308.     superclass-names canonical-slots other-initargs)
  309.   (list 'early-class-definition
  310.     name source metaclass
  311.     superclass-names canonical-slots other-initargs))
  312.   
  313. (defun ecd-class-name        (ecd) (nth 1 ecd))
  314. (defun ecd-source            (ecd) (nth 2 ecd))
  315. (defun ecd-metaclass         (ecd) (nth 3 ecd))
  316. (defun ecd-superclass-names  (ecd) (nth 4 ecd))
  317. (defun ecd-canonical-slots   (ecd) (nth 5 ecd))
  318. (defun ecd-other-initargs    (ecd) (nth 6 ecd))
  319.  
  320. (defvar *early-class-slots* nil)
  321.  
  322. (defun canonical-slot-name (canonical-slot)
  323.   (getf canonical-slot :name))
  324.  
  325. (defun early-class-slots (class-name)
  326.   (cdr (or (assoc class-name *early-class-slots*)
  327.        (let ((a (cons class-name
  328.               (mapcar #'canonical-slot-name
  329.                   (early-collect-inheritance class-name)))))
  330.          (push a *early-class-slots*)
  331.          a))))
  332.  
  333. (defun early-class-size (class-name)
  334.   (length (early-class-slots class-name)))
  335.  
  336. (defun early-collect-inheritance (class-name)
  337.   ;;(declare (values slots cpl default-initargs direct-subclasses))
  338.   (let ((cpl (early-collect-cpl class-name)))
  339.     (values (early-collect-slots cpl)
  340.         cpl
  341.         (early-collect-default-initargs cpl)
  342.         (gathering1 (collecting)
  343.           (dolist (definition *early-class-definitions*)
  344.         (when (memq class-name (ecd-superclass-names definition))
  345.           (gather1 (ecd-class-name definition))))))))
  346.  
  347. (defun early-collect-slots (cpl)
  348.   (let* ((definitions (mapcar #'early-class-definition cpl))
  349.      (super-slots (mapcar #'ecd-canonical-slots definitions))
  350.      (slots (apply #'append (reverse super-slots))))
  351.     (dolist (s1 slots)
  352.       (let ((name1 (canonical-slot-name s1)))
  353.     (dolist (s2 (cdr (memq s1 slots)))
  354.       (when (eq name1 (canonical-slot-name s2))
  355.         (error "More than one early class defines a slot with the~%~
  356.                     name ~S.  This can't work because the bootstrap~%~
  357.                     object system doesn't know how to compute effective~%~
  358.                     slots."
  359.            name1)))))
  360.     slots))
  361.  
  362. (defun early-collect-cpl (class-name)
  363.   (labels ((walk (c)
  364.          (let* ((definition (early-class-definition c))
  365.             (supers (ecd-superclass-names definition)))
  366.            (cons c
  367.              (apply #'append (mapcar #'early-collect-cpl supers))))))
  368.     (remove-duplicates (walk class-name) :from-end nil :test #'eq)))
  369.  
  370. (defun early-collect-default-initargs (cpl)
  371.   (let ((default-initargs ()))
  372.     (dolist (class-name cpl)
  373.       (let* ((definition (early-class-definition class-name))
  374.          (others (ecd-other-initargs definition)))
  375.     (loop (when (null others) (return nil))
  376.           (let ((initarg (pop others)))
  377.         (unless (eq initarg :direct-default-initargs)
  378.          (error "The defclass option ~S is not supported by the bootstrap~%~
  379.                         object system."
  380.             initarg)))
  381.           (setq default-initargs
  382.             (nconc default-initargs (reverse (pop others)))))))
  383.     (reverse default-initargs)))
  384.  
  385. (defun bootstrap-slot-index (class-name slot-name)
  386.   (or (position slot-name (early-class-slots class-name))
  387.       (error "~S not found" slot-name)))
  388.  
  389. ;;;
  390. ;;; bootstrap-get-slot and bootstrap-set-slot are used to access and change
  391. ;;; the values of slots during bootstrapping.  During bootstrapping, there
  392. ;;; are only two kinds of objects whose slots we need to access, CLASSes
  393. ;;; and SLOT-DEFINITIONs.  The first argument to these functions tells whether the
  394. ;;; object is a CLASS or a SLOT-DEFINITION.
  395. ;;;
  396. ;;; Note that the way this works it stores the slot in the same place in
  397. ;;; memory that the full object system will expect to find it later.  This
  398. ;;; is critical to the bootstrapping process, the whole changeover to the
  399. ;;; full object system is predicated on this.
  400. ;;;
  401. ;;; One important point is that the layout of standard classes and standard
  402. ;;; slots must be computed the same way in this file as it is by the full
  403. ;;; object system later.
  404. ;;; 
  405. (defmacro bootstrap-get-slot (type object slot-name)
  406.   `(instance-ref (get-slots ,object) (bootstrap-slot-index ,type ,slot-name)))
  407.  
  408. (defun bootstrap-set-slot (type object slot-name new-value)
  409.   (setf (bootstrap-get-slot type object slot-name) new-value))
  410.  
  411. (defun early-class-name (class)
  412.   (bootstrap-get-slot 'class class 'name))
  413.  
  414. (defun early-class-precedence-list (class)
  415.   (bootstrap-get-slot 'pcl-class class 'class-precedence-list))
  416.  
  417. (defun early-class-name-of (instance)
  418.   (early-class-name (class-of instance)))
  419.  
  420. (defun early-class-slotds (class)
  421.   (bootstrap-get-slot 'slot-class class 'slots))
  422.  
  423. (defun early-slot-definition-name (slotd)
  424.   (bootstrap-get-slot 'standard-effective-slot-definition slotd 'name))
  425.  
  426. (defun early-slot-definition-location (slotd)
  427.   (bootstrap-get-slot 'standard-effective-slot-definition slotd 'location))
  428.  
  429. (defun early-accessor-method-slot-name (method)
  430.   (bootstrap-get-slot 'standard-accessor-method method 'slot-name))
  431.  
  432. (unless (fboundp 'class-name-of)
  433.   (setf (symbol-function 'class-name-of)
  434.     (symbol-function 'early-class-name-of)))
  435.   
  436. (defun early-class-direct-subclasses (class)
  437.   (bootstrap-get-slot 'class class 'direct-subclasses))
  438.  
  439. (proclaim '(notinline load-defclass))
  440. (defun load-defclass
  441.        (name metaclass supers canonical-slots canonical-options accessor-names)
  442.   (setq supers  (copy-tree supers)
  443.     canonical-slots   (copy-tree canonical-slots)
  444.     canonical-options (copy-tree canonical-options))
  445.   (do-standard-defsetfs-for-defclass accessor-names)
  446.   (when (eq metaclass 'standard-class)
  447.     (inform-type-system-about-std-class name))
  448.   (let ((ecd
  449.       (make-early-class-definition name
  450.                        (load-truename)
  451.                        metaclass
  452.                        supers
  453.                        canonical-slots
  454.                        canonical-options))
  455.     (existing
  456.       (find name *early-class-definitions* :key #'ecd-class-name)))
  457.     (setq *early-class-definitions*
  458.       (cons ecd (remove existing *early-class-definitions*)))
  459.     ecd))
  460.  
  461.